package com.dankegongyu.component.jsbridge.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import androidx.annotation.ColorRes;
import androidx.annotation.IdRes;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.core.content.ContextCompat;
import android.view.View;

import com.jaeger.library.StatusBarUtil;

/**
 * Created by Admin on 2018/4/17.
 */

public class JsUtil {

    /**
     * @param activity
     * @param color          颜色
     * @param statusBarAlpha 透明度
     */
    public static void setColor(Activity activity, @ColorRes int color, @IntRange(from = 0, to = 255) int statusBarAlpha) {
        int colorInt = ContextCompat.getColor(activity, color);
        StatusBarUtil.setColor(activity, colorInt, statusBarAlpha);
    }


    /**
     * 设置状态栏全透明
     *
     * @param activity 需要设置的activity
     */
    public static void setTransparent(Activity activity) {
        StatusBarUtil.setTransparent(activity);
    }

    /**
     * 适用于图片作为背景的界面 设置状态栏全透明
     *
     * @param activity 需要设置的activity
     */
    public static void setTransparentForForImageView(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha, View view) {
        StatusBarUtil.setTranslucentForImageView(activity, statusBarAlpha, view);
    }


    /**
     * 使状态栏半透明
     * <p>
     * 适用于图片作为背景的界面,此时需要图片填充到状态栏
     *
     * @param activity       需要设置的activity
     * @param statusBarAlpha 状态栏透明度
     */
    public static void setTranslucent(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha) {
        StatusBarUtil.setTranslucent(activity, statusBarAlpha);
    }


    /**
     * 为滑动返回界面设置状态栏颜色
     *
     * @param activity       需要设置的activity
     * @param color          状态栏颜色值
     * @param statusBarAlpha 状态栏透明度
     */
    public static void setColorForSwipeBack(Activity activity, @ColorRes int color,
                                            @IntRange(from = 0, to = 255) int statusBarAlpha) {
        int colorInt = ContextCompat.getColor(activity, color);
        StatusBarUtil.setColorForSwipeBack(activity, colorInt, statusBarAlpha);

    }


    /**
     * 为 fragment 头部是 ImageView 的设置状态栏透明
     *
     * @param activity       fragment 对应的 activity
     * @param statusBarAlpha 状态栏透明度
     * @param needOffsetView 需要向下偏移的 View
     */
    public static void setTranslucentForInFragment(Activity activity, @IntRange(from = 0, to = 255) int statusBarAlpha,
                                                   View needOffsetView) {
        StatusBarUtil.setTranslucentForImageViewInFragment(activity, statusBarAlpha, needOffsetView);
    }

    /**
     * @param activity
     * @param view
     * @param colorRes
     */
    public static void setPlaceholder(Activity activity, View view, @ColorRes int colorRes) {
        int color = ContextCompat.getColor(activity, colorRes);
        int statusBarHeight = getStatusBarHeight(activity);
        view.setPadding(0, statusBarHeight, 0, 0);
        view.setBackgroundColor(color);
    }


    /**
     * 获取状态栏高度
     *
     * @param context context
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        // 获得状态栏高度
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }

    /**
     * 字图标 变白
     *
     * @param activity
     */
    public static void setLightMode(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusBarUtil.setDarkMode(activity);
        }
    }

    /**
     * 字图标 变黑
     *
     * @param activity
     */
    public static void setDarkMode(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            StatusBarUtil.setLightMode(activity);
        }
    }


    public static void setPlaceholderForTranslucent(Context context, View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int statusBarHeight = getStatusBarHeight(context);
            view.setPadding(0, statusBarHeight, 0, 0);
        }
    }


    public static void setPlaceholderForColor(Context context, View view, @ColorRes int colorRes) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int color = ContextCompat.getColor(context, colorRes);
            int statusBarHeight = getStatusBarHeight(context);
            view.setPadding(0, statusBarHeight, 0, 0);
            view.setBackgroundColor(color);
        }
    }


    public static void addFragmentToActivity(@NonNull FragmentManager fragmentManager, @NonNull Fragment fragment, int frameId) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(frameId, fragment);
//        transaction.commit();
        transaction.commitAllowingStateLoss();

    }


    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * px转dp
     *
     * @param pxValue px值
     * @return dp值
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

}
